<?php

declare(strict_types=1);

/**
 * ExportExcelHandle FILE EXPORT
 */
namespace Service\ExportService;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Service\BaseService;
use Service\Code\Normal;

class ExportExcelHandle extends BaseService
{
    private $sheet;
    private $spreadsheet;
    private $row;
    /**
     * 初始化spread
     */
    public function initSpread()
    {
        // Create new Spreadsheet object
        $this->spreadsheet = new Spreadsheet();
        // Set document properties
        $this->spreadsheet->getProperties()->setCreator('Maarten Balliauw')
        ->setLastModifiedBy('Maarten Balliauw')
        ->setTitle('Office 2007 XLSX Test Document')
        ->setSubject('Office 2007 XLSX Test Document')
        ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
        ->setKeywords('office 2007 openxml php')
        ->setCategory('Test result file');
        // Add some data
        $this->spreadsheet->setActiveSheetIndex(0);
        $this->sheet = $this->spreadsheet->getActiveSheet();
        // Rename worksheet
        $this->spreadsheet->getActiveSheet()->setTitle('Sheet1');
    }
    
    //设置表头
    public function setHeader($title)
    {
        $colum = 'A';
        foreach ($title as $v)
        {
            $this->sheet->setCellValue($colum . '1', $v);
            // $this->sheet->getColumnDimension($colum)->setWidth(40);
            $colum++;
        }
        /* foreach ($title as $key => $item) {
            $this->sheet->setCellValue(chr($key + 65) . '1', $item);
        } */
        $this->row = 2; // 从第二行开始
        return $this;
    }
    
    //添加表内容
    public function addData($data)
    {
        foreach ($data as $item) {
            $dataCol = 'A';
            foreach ($item as $value) {
                // 单元格内容写入
                // $this->sheet->setCellValue($dataCol . $this->row, $value);
                //全部转成字符串 不然 数字就会显示科学计数法
                $this->sheet->setCellValueExplicit($dataCol . $this->row, $value, 's');
                $dataCol++;
            }
            $this->row++;
        }
        return $this;
    }
    
    /**
     * 报文文件内容并且上传阿里云
     * @param string $fileName
     * @param array $header
     * @param array $data
     * @return mixed|int[]|string[]|array[]
     */
    public function uploadAliOssSource(string $fileName,array $header,array $data)
    {
        $pathInfo = pathinfo($fileName);
        if( $pathInfo['extension'] != 'xlsx' ) {
            return Normal::returnCode(Normal::FAIL,"导出数据格式仅支持xlsx");
        }
        
        
        $this->initSpread();
        $this->setHeader($header);
        $this->addData($data);
        $writer = IOFactory::createWriter($this->spreadsheet, "Xlsx");
        
        //保存到服务器的临时文件下
        $temp_file_path = $pathInfo['filename'].'_'.date("YmdHis").rand(10000,99999).'.'.$pathInfo['extension'];
        $writer->save("./".$temp_file_path);
        
        //开始上传
        $result = $this->uploadByTempLocalFile($temp_file_path);
        //删除临时文件
        unlink("./".$temp_file_path);
        return $result;
    }
}